<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*,
com.brother.model.*,com.ibatis.common.util.*,org.apache.commons.lang.*"   pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="international.jsp" %> 

<jsp:useBean id="userService" class="com.brother.service.UserService"></jsp:useBean>

<%

com.brother.model.Customer loginUser = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(loginUser ==null){
	return;
}


%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title><fmt:message key="manage.title.users"/> - <fmt:message key="company.name"/></title>
<link rel="stylesheet" type="text/css"  href="style.css" />
<link rel="stylesheet" type="text/css" href="css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="css/grid-examples.css" />
<link rel="stylesheet" type="text/css" href="css/examples.css" />
<style type="text/css">
.whitebg{
	background-color: #ffffff;
}
body{
background-color:#FFFFFF;
}
</style>

    <script type="text/javascript" src="js/ext-base.js"></script>
    <script type="text/javascript" src="js/ext-all.js"></script>
    <script type="text/javascript" src="js/extexample.js"></script>
    <script type="text/javascript" src="js/search.js"></script>
    <script type="text/javascript" src="./js/common.js"></script>
    
</head>
<body >
<script>
var waitingMsg = '<fmt:message key="waiting.msg"/>';
var waitingProgressText = '<fmt:message key="waiting.loading"/>';
var search_select_user='<fmt:message key="search.select.user"/>';
var currentUserid = '<%=loginUser.getUserid()%>';
var search_select_tracker='<fmt:message key="search.select.tracker"/>';

Ext.onReady(function(){
	Ext.MessageBox.buttonText.yes = '<fmt:message key="button.yes.text"/>';
	Ext.MessageBox.buttonText.no = '<fmt:message key="button.no.text"/>';
	Ext.MessageBox.buttonText.cancel = '<fmt:message key="button.cancel.text"/>';
	
	top.ajustProducts('<fmt:message key="manage.firmware"/>', 450, 200,8);
	document.getElementById("version").value = '';
	document.getElementById("quality").value = '';
});

function submitData(){
	setInfo();
	Ext.Ajax.request({
		method:'POST',
		url: 'transferFirmware.jsp',
		form:'upgrade',
		success:function(res){
    		queryAllStatus(res.responseText, 'transferFirmare');
    	}
	});
}

var setupmsg = '<fmt:message key="parameters.setup.msg"/>';
var setupProgresstext = '<fmt:message key="parameters.setup.progress"/>';
var setSuccess = "<fmt:message key="manage.firmware.read.success"/>";
var setFailed = "<fmt:message key="manage.firmware.read.failed"/>";
var upgradeSuccess = "<fmt:message key="manage.firmware.upgrade.success"/>";
var upgradeFail = "<fmt:message key="manage.firmware.upgrade.failed"/>";
function readVersion(){

	setInfo();
	var trackerName = trackersAutocompleteCombo.getValue();
	Ext.Ajax.request({
		method:'POST',
		url: './reqTrackerVer.jsp',
	    params: {trackerName: trackerName},
	    success:function(res){
	    	queryAllStatus(res.responseText, 'reqTrackerVersion');
	    }
	});
	
}
var failedCount = 15;
/*
 * Query the data received from the tracker through GPRS.
 */
function queryAllStatus(trackerId, action){
	var count = 0;
	
	function fun2(){
		count +=1;

		Ext.Ajax.request({
			url:'queryStatus.jsp',
		    params: 'trackerId='+trackerId+"&action="+action,
		    success:function(res){
				var text = res.responseText;
				//console.log("status888", text);
				if(action == 'reqTrackerVersion'){
					if(text.indexOf("error") > -1){//   error when setting the data
						Ext.MessageBox.hide();
				        Ext.MessageBox.show({
		        	            title: '',
		        	            msg: setFailed,
		        	            buttons: Ext.MessageBox.OK,
		        	            icon: Ext.MessageBox.ERROR
		        	        });
						return;
					}
					if(text.length > 2){
						var datas = text.split(",");
						Ext.MessageBox.hide();
						if(datas.length >= 2){
							Ext.example.msg('<div class="right"></div>', '<div><br/>'+setSuccess+'<div>');
							count = failedCount;
							document.getElementById("version").value = datas[0];
							document.getElementById("quality").value = datas[1];
						}
						return;
					}	
					if(count == failedCount){
				         Ext.MessageBox.hide();
				        // Ext.example.msg('Done', 'Your fake data was saved!');
				        Ext.MessageBox.show({
		        	            title: '',
		        	            msg: setFailed,
		        	            buttons: Ext.MessageBox.OK,
		        	            icon: Ext.MessageBox.ERROR
		        	        });
						//alert(setFailed);
					}
					if(count < failedCount){
						window.setTimeout(fun2, 1000);
						var percent = count / failedCount;
						Ext.MessageBox.updateProgress(percent, Math.round(100*percent)+'%');
					}
				}else{ // transferFirmare
					if(text.indexOf("error") > -1){//   error when setting the data
						Ext.MessageBox.hide();
				        Ext.MessageBox.show({
		        	            title: '',
		        	            msg: upgradeFail,
		        	            buttons: Ext.MessageBox.OK,
		        	            icon: Ext.MessageBox.ERROR
		        	        });
						return;
					}else if(text.indexOf("ok") > -1){
						Ext.MessageBox.hide();
						Ext.example.msg('<div class="right"></div>', '<div><br/>'+upgradeSuccess+'<div>');
						return;
					}else if(text.indexOf("percent") > -1){
						var percent = text.substring(text.indexOf("percent:")+"percent:".length);
						Ext.MessageBox.updateProgress(percent, Math.round(100*Number(percent))+'%');
					}
					window.setTimeout(fun2, 1000);
					
				}
				
		    }
		});
		
		
	}
	
	
	fun2();
}


function setInfo(){
    Ext.MessageBox.show({
        msg: setupmsg,
        progressText: setupProgresstext,
        width:300,
        //wait:true,
        //waitConfig: {interval:200},
        progress:true,
        closable:false
        //icon:'ext-mb-download'
    });
}

</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0">


<tr valign="top">
	<td  align="left" style="padding-left:8px; padding-right:8px;">

<table width="100%" align="left">
	<tr valign="top">
		<td align="left">
			<table  width="100%" align="left">
				<tr>
					<td align="left">
					<form id="upgrade" action="upgrade.jsp" method="post" enctype="multipart/form-data">
					
					<table cellspacing="0" cellpadding="0" style="border-collapse: collapse;border:#85bad8 solid;border-width:1px 0 0 1px">
					<tr>
					<th class="border1_th"><fmt:message key="search.tracker"/>: </th>
					<td class="border1_td"><input type="text" name="trackerName" value=""  id="trackersAutocomplete" style="width:170px"/></td>
					</tr>
				
						
					<tr>
					<th class="border1_th"><fmt:message key="manage.firmware.version"/>: </th>
					<td class="border1_td"><table cellpadding="0" cellspacing="0" border="0"><tr><td><input type="text" name="version" value=""  id="version" style="width:190px"/></td>
					<td style="padding-left:5px"><div id="readBtn"></div>
					<script type="text/javascript">
					var button = new Ext.Button({renderTo:'readBtn',type:'button', 
						text:'<fmt:message key="manage.firmware.read"/>',split: true,width:80,
						enableToggle:false});
						button.on('click', function(){
							if(trackersAutocompleteCombo.getValue() == ''){
								Ext.MessageBox.show({
						            title: '',
						            msg: "<fmt:message key="manage.firmware.tracker.must.choose"/>",
						            buttons: Ext.MessageBox.OK,
						            icon: Ext.MessageBox.WARNING
						        });
								return false;
							}
							readVersion();
						});
					</script></td></tr></table> 
					
					</td>
					</tr>
					
					<tr>
					<th class="border1_th"><fmt:message key="manage.firmware.signal.quality"/>: </th>
					<td class="border1_td"><input type="text" name="quality" value=""  id="quality" style="width:190px"/></td>
					</tr>
					
					<tr>
					<th class="border1_th"><fmt:message key="manage.firmware.file"/>: </th>
					<td  class="border1_td">
					<table cellpadding="0" cellspacing="0" border="0"><tr>
					<td><input type="file" size="21" id="code" name="code"></input></td>
					<td style="padding-left:5px"><div id="submitBtn"></div>
					<script type="text/javascript">
						Ext.onReady(function(){
						var button = new Ext.Button({renderTo:'submitBtn',type:'button', 
							text:'<fmt:message key="manage.firmware.upgrade.submit"/>',split: true,width:80,
							enableToggle:false});
						button.on('click', function(){
								if(document.getElementById("code").value == ''){
									Ext.MessageBox.show({
							            title: '',
							            msg: "<fmt:message key="manage.firmware.file.must.choose"/>",
							            buttons: Ext.MessageBox.OK,
							            icon: Ext.MessageBox.WARNING
							        });
									return false;
								}
								if(trackersAutocompleteCombo.getValue() == ''){
									Ext.MessageBox.show({
							            title: '',
							            msg: "<fmt:message key="manage.firmware.tracker.must.choose"/>",
							            buttons: Ext.MessageBox.OK,
							            icon: Ext.MessageBox.WARNING
							        });
									return false;
								}
								
								submitData();
							});
							});
					</script></td></tr></table>
					</td>
					</tr>
					
					</table>
					</form>
						
						
										
					</td>
				</tr>

								
			
			</table>
		</td>
	</tr>
</table>

	</td>
</tr>

</table>
</body>
</html>
<%
com.brother.database.DataBase.closeThreadConnection();
%>